﻿#define DEBUG

using System;
using System.Collections.Generic;
using System.Text;

using System.Collections;
using System.Runtime.InteropServices;

using SuperMapLib;

namespace JR_RouteAlarm.BufferMaking
{
    class JouetsuNDTakasaki : Common, IRoute 
    {
        private soStrings objStrFields = null;

        public JouetsuNDTakasaki(soStrings objStrFields, soDatasets objDSs, Hashtable routeLnWithBfr)
        {
            this.objStrFields = objStrFields;
            base.objDSs = objDSs;
            base.routeLnWithBfr = routeLnWithBfr;

        }

        #region IRoute メンバ

        public string Feature
        {
            get
            {
                return feature;
            }
            set
            {
                feature = value;
            }
        }

        public string Daibun
        {
            get
            {
                return daibun;
            }
            set
            {
                daibun = value;
            }
        }

        public string Tsuusho
        {
            get
            {
                return tsuusho;
            }
            set
            {
                tsuusho = value;
            }
        }

        public void MakeBufferForRoute(soDatasetVector objVector, soRecordset objRS)
        {
            soRecordset objSubRS = null;

            objRS = objVector.Query(
                FLD_FEATURE_CD + " in ('0102', '0103', '0106') and " +
                FLD_DAIBUN_CD + " in ('01') and " +
                FLD_TSUUSHO_CD + " in ('493', '495')",
                false, objStrFields,
                "group by " + FLD_FEATURE_CD + ", " + FLD_DAIBUN_CD + ", " + FLD_TSUUSHO_CD);

            if (objRS != null && objRS.RecordCount > 0)
            {
                if (!objRS.IsBOF())
                {
                    objRS.MoveFirst();
                }
#if DEBUG
                for (int j = 0; j < objRS.RecordCount; objRS.MoveNext(), j++)
#else
                for (; !objRS.IsEOF(); objRS.MoveNext())
#endif
                {
                    try
                    {
                        feature = objRS.GetFieldValue(FLD_FEATURE_CD).ToString();
                        daibun = objRS.GetFieldValue(FLD_DAIBUN_CD).ToString();
                        tsuusho = objRS.GetFieldValue(FLD_TSUUSHO_CD).ToString();

                        Console.WriteLine(
                            String.Format("{0}:{1},\t{2}:{3},\t{4}:{5}\t---{6}/{7}",
                                FLD_FEATURE_CD, feature,
                                FLD_DAIBUN_CD, daibun,
                                FLD_TSUUSHO_CD, tsuusho,
                                j + 1, objRS.RecordCount));

                        objVector.ClearRecordsets();
                        objSubRS = objVector.Query(
                            FLD_FEATURE_CD + " = '" + feature + "' and " +
                            FLD_DAIBUN_CD + " = '" + daibun + "' and " +
                            FLD_TSUUSHO_CD + " = '" + tsuusho + "'",
                            true, null, "");

                        int multiLineCount = objSubRS.RecordCount;
                        if (objSubRS != null && multiLineCount > 0)
                        {
                            if (!objSubRS.IsBOF())
                            {
                                objSubRS.MoveFirst();
                            }

                            soGeoLine objLineTmp = null;
                            soGeoLine[] objMultiLines = new soGeoLine[Utility.MAX_MULTIPLE_LINES]; //複数非連続線路
                            int i = 0;
                            for (; !objSubRS.IsEOF(); objSubRS.MoveNext())
                            {
                                objLineTmp = objSubRS.GetGeometry() as soGeoLine;
                                objMultiLines[i++] = objLineTmp;
                            }
                            objSubRS.Close();
                            Marshal.ReleaseComObject(objLineTmp);
                            objLineTmp = null;

                            routeInfo = new RouteArea(objDSs["キロ標_高崎"]);
                            routeInfo.SetInfo(feature, daibun, tsuusho);

                            //複数非連続線路
                            for (int k = 0; k < i; k++)
                            {
                                Console.WriteLine(
                                    String.Format("{0}:{1},\t{2}:{3},\t{4}:{5}\t---{6}/{7}---\t{1},{3},{5}",
                                        FLD_FEATURE_CD, feature, FLD_DAIBUN_CD, daibun, FLD_TSUUSHO_CD, tsuusho,
                                        k + 1, multiLineCount));

                                MakeBufferForPart(objMultiLines[k]);
                            }

                            Marshal.ReleaseComObject(objLineTmp);
                            objLineTmp = null;
                            Marshal.ReleaseComObject(objSubRS);
                            objSubRS = null;
                        }
                    }
                    catch (Exception ex)
                    {
                        objRS.Close();
                        Marshal.ReleaseComObject(objSubRS);
                        objSubRS = null;

                        throw new Exception(ex.Message);
                    }
                }

                objRS.Close();
                Marshal.ReleaseComObject(objSubRS);
                objSubRS = null;
            }
        }

        #endregion
    }
}
